package cn.cloudtogo.server.mysql.controller;

import cn.cloudtogo.common.atomapi.ApiToken;
import cn.cloudtogo.common.types.Pair;
import cn.cloudtogo.support.mysql.Condition;
import cn.cloudtogo.support.mysql.table.TableRowApi;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static cn.cloudtogo.common.atomapi.ApiDisplay.*;

/**
 * @author yaolianhua789@gmail.com
 * <p>
 * 方法和参数上的注释是api接口的相关定义，是必要配置
 * <p>
 * {@link cn.cloudtogo.server.mysql.service.ApiListService#query(String, String, String)}
 **/
@RestController
@RequestMapping("/api/{uuid}/{table}")
public class TableRowsController {

    private final TableRowApi tableRowApi;

    public TableRowsController(TableRowApi tableRowApi) {
        this.tableRowApi = tableRowApi;
    }

    /*@ApiOperation(value = "往表中写入一行数据", code = 201, response = Integer.class, notes = "insert_data")
    @PostMapping
    public ResponseEntity<Result<Integer>> post(@ApiParam(name = "pair", required = true, value = "body请求参数", type = "json", example = "{\n" +
            "  \"property-1\": {},\n" +
            "  \"property-2\": {},\n" +
            "  \"property-3\": {}\n" +
            "}") @RequestBody Pair pair,
                                                @PathVariable(UUID) String uuid,
                                                @PathVariable(TABLE) String table) {
        int insert = tableRowApi.insert(uuid, table, pair);
        return created(insert);
    }*/

    @ApiToken
    @ApiOperation(
            value = "新增数据",
            code = 201,
            response = List.class,
            httpMethod = "POST",
            notes = "insert_data"
    )
    @ApiImplicitParams(
            @ApiImplicitParam(
                    name = "Authorization",
                    value = "授权",
                    paramType = "header",
                    example = "Zys85_k1fRUEgWFQGC9OINtafdHfn_vsbuyOd9EpxJGcEUKozU6cVUaALCOFa6mC2yJD5dQk27Rmh3BStJzQjlCvCduPTOw",
                    required = true,
                    dataTypeClass = String.class
            )
    )
    @PostMapping
    public ResponseEntity<List<Pair>> postBatch(
            @ApiParam(
                    name = "pairs",
                    required = true,
                    value = "body请求参数",
                    type = "json",
                    example = "[{\n" +
                            "  \"property-1\": {},\n" +
                            "  \"property-2\": {},\n" +
                            "  \"property-3\": {}\n" +
                            "}]")
            @RequestBody List<Pair> pairs,
            @PathVariable(UUID) String uuid,
            @PathVariable(TABLE) String table) {
        List<Pair> result = tableRowApi.insertBatch(uuid, table, pairs.toArray(new Pair[]{}));
        return ResponseEntity.accepted().body(result);
    }

    @ApiToken
    @ApiOperation(
            value = "查询单行数据",
            code = 200,
            response = Pair.class,
            httpMethod = "GET",
            notes = "get_data_byId"
    )
    @ApiImplicitParams(
            @ApiImplicitParam(
                    name = "Authorization",
                    value = "授权",
                    paramType = "header",
                    example = "Zys85_k1fRUEgWFQGC9OINtafdHfn_vsbuyOd9EpxJGcEUKozU6cVUaALCOFa6mC2yJD5dQk27Rmh3BStJzQjlCvCduPTOw",
                    required = true,
                    dataTypeClass = String.class
            )
    )
    @GetMapping("/{id}")
    public ResponseEntity<Pair> get(
            @ApiParam(
                    value = "主键ID",
                    type = "string",
                    required = true,
                    name = ID,
                    example = "f9188401-bbe3-4b62-a5be-b2a7fa622a3d"
            )
            @PathVariable(ID) String id,
            @PathVariable(UUID) String uuid,
            @PathVariable(TABLE) String table) {
        Pair pair = tableRowApi.find(uuid, table, id);

        return ResponseEntity.ok(pair);
    }

    @ApiToken
    @ApiOperation(
            value = "查询多行数据",
            code = 200,
            response = List.class,
            httpMethod = "GET",
            notes = "get_data_list"
    )
    @ApiImplicitParams(
            @ApiImplicitParam(
                    name = "Authorization",
                    value = "授权",
                    paramType = "header",
                    example = "Zys85_k1fRUEgWFQGC9OINtafdHfn_vsbuyOd9EpxJGcEUKozU6cVUaALCOFa6mC2yJD5dQk27Rmh3BStJzQjlCvCduPTOw",
                    required = true,
                    dataTypeClass = String.class
            )
    )
    @GetMapping
    public ResponseEntity<List<Pair>> get(
            @ApiParam(
                    name = FIELDS,
                    value = "需要筛选的字段",
                    required = false,
                    example = "field1,field2,field ...",
                    type = "string"
            )
            @RequestParam(required = false) String fields,
            @ApiParam(
                    name = PAGE,
                    value = "当前页",
                    example = "1",
                    required = false,
                    type = "int"
            )
            @RequestParam(required = false) Integer page,
            @ApiParam(
                    name = SIZE,
                    value = "条目数",
                    example = "10",
                    required = false,
                    type = "int")
            @RequestParam(required = false) Integer size,
//                                                  @ApiParam(name = WHERE, value = "sql查询条件", example = "id > 100 and name='jack'", required = false, type = "string") @RequestParam(required = false) String where,
            @PathVariable(UUID) String uuid,
            @PathVariable(TABLE) String table) {
        Condition condition = Condition.of(fields, page, size, null);
        List<Pair> pairs = tableRowApi.find(uuid, table, condition);
        return ResponseEntity.ok(pairs);
    }

    @ApiToken
    @ApiOperation(
            value = "查询数据行数",
            code = 200,
            response = Integer.class,
            httpMethod = "GET",
            notes = "get_data_count"
    )
    @ApiImplicitParams(
            @ApiImplicitParam(
                    name = "Authorization",
                    value = "授权",
                    paramType = "header",
                    example = "Zys85_k1fRUEgWFQGC9OINtafdHfn_vsbuyOd9EpxJGcEUKozU6cVUaALCOFa6mC2yJD5dQk27Rmh3BStJzQjlCvCduPTOw",
                    required = true,
                    dataTypeClass = String.class
            )
    )
    @GetMapping("/count")
    public ResponseEntity<Integer> count(@PathVariable(UUID) String uuid,
//                                                 @ApiParam(name = CONDITION, value = "sql查询的where条件", example = "id > 10 and name='jack'", type = "string", required = false) @RequestParam(required = false) String condition,
                                         @PathVariable(TABLE) String table) {
        int count = tableRowApi.count(uuid, table, null);
        return ResponseEntity.ok(count);
    }

    @ApiToken
    @ApiOperation(
            value = "更新单行数据",
            code = 202,
            response = Pair.class,
            httpMethod = "PUT",
            notes = "update_data_byId"
    )
    @ApiImplicitParams(
            @ApiImplicitParam(
                    name = "Authorization",
                    value = "授权",
                    paramType = "header",
                    example = "Zys85_k1fRUEgWFQGC9OINtafdHfn_vsbuyOd9EpxJGcEUKozU6cVUaALCOFa6mC2yJD5dQk27Rmh3BStJzQjlCvCduPTOw",
                    required = true,
                    dataTypeClass = String.class
            )
    )
    @PutMapping("/{id}")
    public ResponseEntity<Pair> put(
            @ApiParam(
                    name = "pair",
                    required = true,
                    value = "body请求参数",
                    type = "json",
                    example = "{\n" +
                            "  \"property-1\": {},\n" +
                            "  \"property-2\": {},\n" +
                            "  \"property-3\": {}\n" +
                            "}")
            @RequestBody Pair pair,
            @PathVariable(UUID) String uuid,
            @ApiParam(
                    value = "主键ID",
                    type = "string",
                    name = ID,
                    example = "f9188401-bbe3-4b62-a5be-b2a7fa622a3d",
                    required = true
            )
            @PathVariable(ID) String id,
            @PathVariable(TABLE) String table) {
        Pair data = tableRowApi.update(uuid, table, id, pair);
        return ResponseEntity.accepted().body(data);
    }

    @ApiToken
    @ApiOperation(
            value = "删除单行数据",
            code = 202,
            response = Integer.class,
            httpMethod = "DELETE",
            notes = "delete_data_byId"
    )
    @ApiImplicitParams(
            @ApiImplicitParam(
                    name = "Authorization",
                    value = "授权",
                    paramType = "header",
                    example = "Zys85_k1fRUEgWFQGC9OINtafdHfn_vsbuyOd9EpxJGcEUKozU6cVUaALCOFa6mC2yJD5dQk27Rmh3BStJzQjlCvCduPTOw",
                    required = true,
                    dataTypeClass = String.class
            )
    )
    @DeleteMapping("/{id}")
    public ResponseEntity<Integer> delete(
            @ApiParam(
                    value = "主键ID",
                    type = "string",
                    name = ID,
                    example = "f9188401-bbe3-4b62-a5be-b2a7fa622a3d",
                    required = true
            )
            @PathVariable(ID) String id,
            @PathVariable(UUID) String uuid,
            @PathVariable(TABLE) String table) {
        int delete = tableRowApi.delete(uuid, table, id);
        return ResponseEntity.accepted().body(delete);
    }

    @ApiToken
    @ApiOperation(
            value = "删除多行数据",
            code = 202,
            response = Integer.class,
            httpMethod = "DELETE",
            notes = "delete_data_list"
    )
    @ApiImplicitParams(
            @ApiImplicitParam(
                    name = "Authorization",
                    value = "授权",
                    paramType = "header",
                    example = "Zys85_k1fRUEgWFQGC9OINtafdHfn_vsbuyOd9EpxJGcEUKozU6cVUaALCOFa6mC2yJD5dQk27Rmh3BStJzQjlCvCduPTOw",
                    required = true,
                    dataTypeClass = String.class
            )
    )
    @DeleteMapping
    public ResponseEntity<Integer> delete(@PathVariable(UUID) String uuid,
                                          @PathVariable(TABLE) String table,
                                          @ApiParam(
                                                  name = PRIMARY_KEY,
                                                  value = "主键的字段名(不支持复合主键),可为空",
                                                  example = "flow_id",
                                                  type = "string",
                                                  required = false
                                          )
                                          @RequestParam(value = PRIMARY_KEY, required = false) String primaryKey,
                                          @ApiParam(
                                                  name = ID_COMMA,
                                                  value = "逗号分隔的主键ID字符串",
                                                  example = "id1,id2,id3 ...",
                                                  type = "string",
                                                  required = true
                                          )
                                          @RequestParam(ID_COMMA) String idComma) {
        int batch = tableRowApi.deleteBatch(uuid, table, primaryKey, idComma);
        return ResponseEntity.accepted().body(batch);
    }

}
